package com.weiss.jpro.config;

import com.weiss.jpro.utils.LoginInterceptor;
import com.weiss.jpro.utils.RefreshTokenInterceptor;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.annotation.Resource;

// 实现webmvcconfiger
@Configuration
public class MvcConfig implements WebMvcConfigurer {

    // 这里加了Configuration注解，可以用注解进行注入
    @Resource
    private StringRedisTemplate stringRedisTemplate;

    // 添加拦截器
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        // 拦截器1 token刷新拦截器
        registry.addInterceptor(new RefreshTokenInterceptor(stringRedisTemplate))
                .addPathPatterns("/**")
                .order(0); // 拦截器执行顺序
        // 拦截器2 登录拦截器
        registry.addInterceptor(new LoginInterceptor())
                .excludePathPatterns( // 配置拦截路径，默认全部拦截,排除不需要的拦截路径
                        "/user/code",
                        "/user/login",
                        "/blog/hot",
                        "/shop/**",
                        "/shop-type/**",
                        "/upload/**",
                        "/voucher/**"
                )
                .order(1); // 拦截器执行顺序

//        // registry：拦截器的注册器，利用它添加LoginInterceptor
//        registry.addInterceptor(new LoginInterceptor(stringRedisTemplate))
//                .excludePathPatterns( // 配置拦截路径，默认全部拦截,排除不需要的拦截路径
//                        "/user/code",
//                        "/user/login",
//                        "/blog/hot",
//                        "/shop/**",
//                        "shop-type/**",
//                        "/upload/**",
//                        "/voucher/**"
//                );
    }
}
